﻿using SelfMadeMillionaire.Core.Entities;
using SelfMadeMillionaire.Core.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SelfMadeMillionaire.Core.Specifications.Newss
{
	public class RecentNewsSpecification : ISearchSpecification<News>
	{
		private int _count;
		private ProductType _productType;
        SubscriptionType minAccessLevel;

        public RecentNewsSpecification(int showCount, ProductType productType, SubscriptionType minAccessLevel)
		{
			_count = showCount;
			_productType = productType;
            this.minAccessLevel = minAccessLevel;
		}

        public IEnumerable<News> SatisfiedBy(IQueryable<News> entities)
		{
			return entities.Where(x => x.Status == (byte)StatusType.Active

                && (
                    (x.ProductType == (byte)_productType) ||
                    (x.Products.Any(p => p.ProductType == (byte)_productType))
                )

                && x.MinAccessLevel <= (byte)minAccessLevel)
				.OrderByDescending(x => x.Date)
				.Take(_count);
		}
	}
}
